package com.meiqiongproperty.yiguanjia.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.meiqiongproperty.yiguanjia.db.MyDB;
import com.meiqiongproperty.yiguanjia.db.table.ShoppingCarTable;
import com.meiqiongproperty.yiguanjia.httpbean.shopcart.ShoppingCarBean;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;

import java.util.ArrayList;
import java.util.Locale;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.db.dao
 * @shop ChenJing
 * @Date 2017/6/18
 */

public class ShoppingCarDao implements ShoppingCarTable{
    private static final String TAG = ShoppingCarTable.class.getSimpleName();

    private SQLiteDatabase mDb;

    /**
     * 将上层的context传到Dao层
     *
     * @param context
     */
    public ShoppingCarDao(Context context) {
        mDb = MyDB.getInstance(context).getDB();
        if (mDb == null) {
            Log.e(TAG, "init SQLiteDatabase failed ");
        }
    }

    /**
     * 添加一条
     *
     * @param shop
     * @return
     */
    public long add(ShoppingCarBean shop) {
        // 入库成功后的id。
        long id = -1;
        if (null == shop) {
            Log.w(TAG, "Add note:note is null!");
            return id;
        }
        // 入库数值。
        ContentValues values = new ContentValues();
        values.put(PRODUCT_COUNT, shop.getProduct_count());
        values.put(PRODUCT_ID, shop.getProduct_id());
        values.put(PRODUCT_IMAGE, shop.getProduct_image());
        values.put(PRODUCT_OPRICE, shop.getProduct_oprice());
        values.put(PRODUCT_TITLE, shop.getProduct_title());
        values.put(PRODUCT_PRICE,shop.getProduct_price());
        LogUtil.d("mDb：" + mDb);
        // 入库。
        id = mDb.insert(TABLE_NAME, null, values);
        return id;
    }

    /**
     * 添加多条
     *
     * @param data
     * @return
     */
    public long addAll(ArrayList<ShoppingCarBean> data) {
        // 入库成功后的id。
        long id = -1;
        if (null == data) {
            Log.w(TAG, "Add note:note is null!");
            return id;
        }
        // 入库数值。
        ContentValues values = new ContentValues();
        for (int i=0;i<data.size();i++){
            values.put(PRODUCT_COUNT, data.get(i).getProduct_count());
            values.put(PRODUCT_ID, data.get(i).getProduct_id());
            values.put(PRODUCT_IMAGE, data.get(i).getProduct_image());
            values.put(PRODUCT_OPRICE, data.get(i).getProduct_oprice());
            values.put(PRODUCT_TITLE, data.get(i).getProduct_title());
            values.put(PRODUCT_PRICE,data.get(i).getProduct_price());
            LogUtil.d("mDb：" + mDb);
            // 入库。
            id = mDb.insert(TABLE_NAME, null, values);
        }
        return id;
    }

    /**
     * 通过ID更新
     *
     * @param id
     * @param shop
     * @return
     */
    public int update(int id, ShoppingCarBean shop) {
        int updateCount = 0;
        // 参数检查
        if (id < 0 || (shop == null)) {
            Log.w(TAG, "update note,id = " + id);
            return updateCount;
        }
        // 匹配条件
        String whereClause = String.format("%s=?", ShoppingCarTable.PRODUCT_ID);
        String[] whereArgs = new String[]{String.valueOf(id)};
        // 入库值
        ContentValues values = new ContentValues();
        values.put(PRODUCT_COUNT, shop.getProduct_count());
        values.put(PRODUCT_ID, shop.getProduct_id());
        values.put(PRODUCT_IMAGE, shop.getProduct_image());
        values.put(PRODUCT_OPRICE, shop.getProduct_oprice());
        values.put(PRODUCT_TITLE, shop.getProduct_title());
        values.put(PRODUCT_PRICE,shop.getProduct_price());
        try {
            updateCount = mDb.update(ShoppingCarTable.TABLE_NAME, values, whereClause,
                    whereArgs);
        } catch (Exception e) {
            Log.e(TAG, "update note table error! count = " + updateCount);
        }
        return updateCount;
    }


    /**
     * 通过ID删除
     *
     * @param shop_id
     * @return
     */
    public int delete(String shop_id) {
        // 创建返回值
        int delCount = 0;
        // 参数检查
//        if (id < 0) {
//            Log.w(TAG, "query note,id = " + id);
//            return delCount;
//        }
        // 匹配条件
        String whereClause = String.format("%s=?", PRODUCT_ID);
        String[] whereArgs = new String[]{shop_id};
        try {
            delCount = mDb.delete(ShoppingCarTable.TABLE_NAME, whereClause, whereArgs);
        } catch (Exception e) {
            Log.e(TAG, "delete note table error! id =" + delCount);
        }
        return delCount;
    }

    /**
     * 删除全部
     *
     * @return
     */
    public int deleteAll() {
        int delCount = 0;
        try {
            delCount = mDb.delete(ShoppingCarTable.TABLE_NAME, null, null);

        } catch (Exception e) {
            Log.e(TAG, "delete note table error! id =" + delCount);
        }
        return delCount;
    }

    /**
     * 通过ID查询
     *
     * @param id
     * @return
     */
    public ShoppingCarBean query(int id) {
        // 参数检查
        if (id < 0) {
            Log.w(TAG, "query note,id = " + id);
            return null;
        }
        // 创建返回值
        ShoppingCarBean shop = null;
        // 查询条件
        String selection = String.format(Locale.US, "%s='%s'", PRODUCT_ID, id);
        // 获取游标
        Cursor cursor = null;
        try {
            cursor = mDb.query(TABLE_NAME, null, selection, null, null, null,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                // 将数值设置到实体类中
                shop = new ShoppingCarBean();
                shop.setProduct_id(cursor.getString(INDEX_PRODUCT_ID));
                shop.setProduct_count(cursor.getString(INDEX_PRODUCT_COUNT));
                shop.setProduct_image(cursor.getString(INDEX_PRODUCT_IMAGE));
                shop.setProduct_price(cursor.getString(INDEX_PRODUCT_PRICE));
                shop.setProduct_oprice(cursor.getString(INDEX_PRODUCT_OPRICE));
                shop.setProduct_title(cursor.getString(INDEX_PRODUCT_TITLE));
            }
        } catch (SQLiteException e) {
            Log.e(TAG, "---Query failed!" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return shop;
    }

    /**
     * 查询全部
     *
     * @return
     */
    public ArrayList<ShoppingCarBean> queryAll() {
        // 创建返回值
        ArrayList<ShoppingCarBean> shopList = new ArrayList<ShoppingCarBean>();
        // 查询条件
        String order = PRODUCT_ID + " DESC";
        // 获取游标
        Cursor cursor = null;
        ShoppingCarBean shop = null;
        try {
            cursor = mDb.query(TABLE_NAME, null, null, null, null, null, order);
            if (cursor != null && cursor.moveToFirst()) {
                // 循环取出所有数值
                do {
                    // 赋值
                    shop = new ShoppingCarBean();
                    shop.setProduct_id(cursor.getString(INDEX_PRODUCT_ID));
                    shop.setProduct_count(cursor.getString(INDEX_PRODUCT_COUNT));
                    shop.setProduct_image(cursor.getString(INDEX_PRODUCT_IMAGE));
                    shop.setProduct_price(cursor.getString(INDEX_PRODUCT_PRICE));
                    shop.setProduct_oprice(cursor.getString(INDEX_PRODUCT_OPRICE));
                    shop.setProduct_title(cursor.getString(INDEX_PRODUCT_TITLE));
                    // 添加到集合中
                    shopList.add(shop);
                } while (cursor.moveToNext());
            }
        } catch (SQLiteException e) {
            Log.e(TAG, "Query all note failed!" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return shopList;
    }
}
